// pages/item/index.js
// 商品详情页面
const app = getApp()
Page({
    data: {
        id: 0,
        item: {},
        proItem: ["极速发货", "正品保障", "实力商家", "退换包运费"],
        isShow: true,
        url: app.globalData.url
    },
    onShow() {
        // 获取登录用户信息
        const user = wx.getStorageSync("user")
        if (user) {
            this.setData({ user })
        }
    },
    onLoad(options) {
        if (options) {
            let item = JSON.parse(options.item)
            this.setData({ item })
            wx.setNavigationBarTitle({
                title: item.name,
            })
        }
    },
    // 用户登录
    to_login() {
        const appid = "wx8efd630c320af804"
        const secret = "1174ac6e704e3ca93e12eb5b5eeec336"
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    // 将登录凭证发送到服务端
                    wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid='
                            + appid + '&secret=' + secret + '&js_code=' + res.code
                            + '&grant_type=authorization_code',
                        method: 'GET',
                        success(res) {
                            wx.request({
                                url: that.data.url + "/user/login",
                                data: {
                                    openid: res.data.openid
                                },
                                success(res) {
                                    if (res.data.code == 1) {
                                        console.log(res.data.result);
                                        wx.setStorageSync("user", res.data.result)
                                        that.setData({ user: res.data.result })
                                        wx.showToast({ title: '登录成功' })
                                        const pages = getCurrentPages()
                                        const perpage = pages[pages.length - 1]
                                        perpage.onLoad()
                                    }
                                }
                            })
                        }
                    })
                }
            }
        })
    },
    toCar() {
        wx.switchTab({
            url: '/pages/car/index',
        })
    },
    // 点击加入购物车按钮
    addCar() {
        this.setData({
            isShow: false
        })
    },
    handleCancle() {
        this.setData({
            isShow: true
        })
    },
    // 电话
    call() {
        wx.makePhoneCall({
            phoneNumber: '123-4567-8901',
            success() {
                console.log("success");
            },
            fail() {
                console.log("fail");
            }
        })
    },
    // 加入购物车
    addToCar(e) {
        wx.showLoading({
            title: '请稍后...',
        })
        const that = this
        if (!that.data.user) {
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showModal({
                title: 'tips',
                content: '请先登录',
                success(res) {
                    if (res.confirm) {
                        that.to_login()
                    } else if (res.cancel) {
                        wx.showToast({
                            title: '取消登录',
                        })
                        return
                    }
                }
            })
        }
        if (e.detail) {
            // 发送到后端加入数据库
            wx.request({
                url: that.data.url + '/car/add',
                data: {
                    uid: that.data.user.id,
                    gid: e.detail.id
                },
                success(res) {
                    if (res.data.code == 1) {
                        wx.hideLoading({
                            success: (res) => {
                                wx.showToast({
                                    title: '添加成功',
                                    success() {
                                        wx.reLaunch({
                                            url: '/pages/car/index',
                                        })
                                    }
                                })
                            },
                        })

                    } else {
                        wx.hideLoading({
                            success: (res) => {},
                          })
                        wx.showModal({
                            title: '错误',
                            content: res.data.msg,
                            success(res) {
                                if (res.confirm) { } else if (res.cancel) { }
                            }
                        })
                    }
                }
            })
        }
    },
    payNow() {
        this.setData({
            isShow: false,
            type: 'pay'
        })
    },
    // 立即购买
    pay(e) {
        wx.showLoading({
            title: '请稍后...',
        })
        const that = this
        if (!that.data.user) {
            wx.showModal({
                title: 'tips',
                content: '请先登录',
                success(res) {
                    if (res.confirm) {
                        that.to_login()
                    } else if (res.cancel) {
                        wx.showToast({
                            title: '取消登录',
                        })
                        return
                    }
                }
            })
        }
        if (e.detail) {
            console.log("payyyyy");
            // 发送到后端生成订单对象
            wx.request({
                url: that.data.url + '/kill/sc',
                data: {
                    uid: that.data.user.id,
                    gid: e.detail.id,
                },
                success(res) {
                    if (res.data.code == 1) {
                        let orders = res.data.result
                        let list = []
                        list.push(orders)
                        wx.hideLoading({
                            success: (res) => {
                                wx.navigateTo({
                                    url: '/pages/order/index?list=' + JSON.stringify(list) + "&total=" + e.detail.killPrice,
                                })
                            },
                        })
                    } else {
                        wx.hideLoading({
                          success: (res) => {},
                        })
                        wx.showModal({
                            title: '错误',
                            content: res.data.msg,
                            success(res) {
                                if (res.confirm) { } else if (res.cancel) { }
                            }
                        })
                    }
                }
            })
        }
    }
})